package com.xbongbong.paas.service.impl;

import com.xbongbong.paas.pojo.dto.UserConfigAddDTO;
import com.xbongbong.paas.service.GuideLableService;
import com.xbongbong.paas.service.UserConfigService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.config.pojo.dto.GuideLabelIsShowDTO;
import com.xbongbong.pro.config.pojo.dto.GuideLabelSetValueDTO;
import com.xbongbong.pro.config.pojo.vo.GuideLabelIsShowVO;
import com.xbongbong.pro.config.pojo.vo.GuideLabelSetValueVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.help.AppModuleHelp;
import com.xbongbong.saas.model.AppModuleModel;
import com.xbongbong.sys.domain.entity.CompanyEntity;
import com.xbongbong.sys.domain.entity.UserConfigEntity;
import com.xbongbong.sys.enums.UserConfigEnum;
import com.xbongbong.sys.model.CompanyModel;
import com.xbongbong.sys.model.UserConfigModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author: chy
 * @date: 2019/02/21 10:21
 * @desrcption: web首页上手攻略设置
 */
@Service("guideLableService")
public class GuideLableServiceImpl implements GuideLableService {

    private static final Logger LOG = LoggerFactory.getLogger(GuideLableServiceImpl.class);

    @Resource
    private UserConfigService userConfigService;
    @Resource
    private UserConfigModel userConfigModel;
    @Resource
    private CompanyModel companyModel;
    @Resource
    private AppModuleModel appModuleModel;
    @Resource
    private AppModuleHelp appModuleHelp;

    @Override
    public GuideLabelSetValueVO guideLabelSetValue(GuideLabelSetValueDTO guideLabelSetValueDTO) throws XbbException {
        GuideLabelSetValueVO guideLabelSetValueVO = new GuideLabelSetValueVO();
        try {
            // 保存到数据库中
            UserConfigAddDTO userConfigAddDTO = new UserConfigAddDTO();
            BeanUtil.copyProperties(guideLabelSetValueDTO, userConfigAddDTO);
            userConfigAddDTO.setConfigName(UserConfigEnum.SHOW_GUIDE_LABEL.getName());
            userConfigAddDTO.setConfigAlias(UserConfigEnum.SHOW_GUIDE_LABEL.getAlias());

            userConfigService.save(userConfigAddDTO);
        } catch (Exception e) {
            LOG.error("上手攻略设置出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return guideLabelSetValueVO;
    }

    @Override
    public GuideLabelIsShowVO guideLabelIsShow(GuideLabelIsShowDTO guideLabelIsShowDTO) throws XbbException {
        GuideLabelIsShowVO guideLabelIsShowVO = new GuideLabelIsShowVO();
        try {
            String userId = guideLabelIsShowDTO.getUserId();
            String corpid = guideLabelIsShowDTO.getCorpid();

            CompanyEntity company = companyModel.getByKey(corpid);

            //公司不存在
            if (company == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100011, SystemErrorCodeEnum.API_ERROR_100011.getMsg());
            }

            //判断公司创建时间
            Long companyCreateTime = company.getAddTime();

            //TODO 这边的时间需要调整
            Long proDeployTime = DateTimeUtil.getInt("2010-03-15", DateTimeUtil.SDFDate);

            //创建时间 < pro部署时间
            if(companyCreateTime < proDeployTime){
                guideLabelIsShowVO.setShow(false);
                return guideLabelIsShowVO;
            }

            UserConfigEntity userConfigEntity = userConfigModel.getByUserId(userId, corpid,
                    UserConfigEnum.SHOW_GUIDE_LABEL.getAlias());

            if (userConfigEntity == null || Objects.equals(userConfigEntity.getConfigValue(), "1")) {
                guideLabelIsShowVO.setShow(true);
            } else {
                guideLabelIsShowVO.setShow(false);
            }

            appModuleModel.getAllList().stream().filter(item -> Objects.equals(item.getShelves(), 1)).forEach(item -> {
                // 展示“new”标签的处理
                if (appModuleHelp.showNewLabel(item, item.getId() + corpid + userId)) {
                    guideLabelIsShowVO.setIsNew(true);
                }
            });
        } catch (Exception e) {
            LOG.error("获取上手攻略是否显示失败", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return guideLabelIsShowVO;
    }


}
